package me.aip.mov.task;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import me.aip.mov.entity.Type;
import me.aip.mov.entity.Vod;
import me.aip.mov.service.ITypeService;
import me.aip.mov.service.IVodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description
 * @Author sgl
 * @Date 2018-06-26 10:07
 */
@Component
@Slf4j
public class task {

    private static final long SECOND = 1000;

    /**
     * 注入service
     */
    @Autowired
    private IVodService iVodService;
    @Autowired
    private ITypeService iTypeService;

    /**
     * 固定间隔3秒，可以引用变量
     * fixedRate：以每次开始时间作为测量，间隔固定时间
     */
    @Scheduled(fixedRate = 60*5*SECOND)
    public void task() {
        List<Type> labels = iTypeService.findLabels();
        this.cacheLabelVodList(labels);
        this.cacheCasual(labels);
        this.cacheGuessVodList();
        this.cacheTjVodList(labels);
        this.cacheCateData(labels,1);
    }

    private void cacheTjVodList(List<Type> labels) {
        labels.forEach(t->{
            iVodService.recommendVod(null,t.getTypeId(),null);
        });
        iVodService.labelVodList(0);
    }

    private void cacheGuessVodList() {
        iVodService.guessVod("");
    }
    private void cacheLabelVodList(List<Type> labels) {
        labels.forEach(t->{
            iVodService.labelVodList(t.getTypeId());
        });
        iVodService.labelVodList(0);
    }


    private void cacheCasual(List<Type> labels){
        labels.forEach(t->{
            iVodService.findCarousel(t.getTypeId());
        });
        iVodService.findCarousel(0);
        log.info("执行轮播图完成:"+ LocalDateTime.now());
    }

    private void cacheCateData(List<Type> labels,Integer cachePage){
        Page page = new Page();
        int year = LocalDate.now().getYear();
        page.setSize(30);
        page.setCurrent(cachePage);
        Vod vod = new Vod();
        vod.setVodYear("");
        iVodService.findVodPage(page,vod);
        labels.forEach(t->{
            vod.setTypePid(t.getTypeId());
            iVodService.findVodPage(page,vod);

            List<Type> types = iTypeService.findTypes(t.getTypeId());
            types.forEach(s->{
                    vod.setTypeId(s.getTypeId());
                    iVodService.findVodPage(page,vod);
                    for (int j=year;j > year-15;j--){
                        vod.setVodYear(String.valueOf(j));
                        iVodService.findVodPage(page,vod);
                    }

            });
            vod.setTypeId(null);
            for (int j=year;j > year-15;j--){
                vod.setVodYear(String.valueOf(j));
                iVodService.findVodPage(page,vod);
            }
        });
        log.info("分类数据缓存完成:"+ LocalDateTime.now());
    }

}
